<?php

namespace App\Http\Controllers\Admins\Appointment;

use App\Http\Controllers\Admins\BaseAdminController;
use App\Models\OperationalLogs;
use App\Models\SchoolGate;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class SchoolGateController extends BaseAdminController
{
    /**
     * 校门列表
     *
     * @param Request $request
     * @return array|Application|Factory|View
     *
     */
    public function index(Request $request){
        $condition = $request->get('condition','name');
        if($request->isMethod('post')){
            # 要执行的代码
            $query = SchoolGate::query()->latest();
            $keywords = $request->get('keywords',null);
            if ($keywords) {
                $query->where($condition, 'like', '%' . $keywords . '%');
            }
            $status = $request->get('status','all');
            if($status != 'all'){
                $query->where('status', $status);
            }
            $pageSize = $request->get('limit',10);
            # 计算offset值
            $offset = $request->get('offset',0);
            # 总数
            $total = $query->count();
            $data = $query->limit($pageSize)->offset($offset)->get();
            $result = array();
            if($data){
                foreach($data as $val){
                    $result[] = SchoolGate::getDataByID($val['id']);
                }
            }
            return ret([
                'total'=>$total,
                'data'=>$result
            ]);
        }
        $admin_id = $request->get('admin_id');
        $subtitle = '校门管理';
        $compact = compact('subtitle','admin_id');
        return view('v1.admin.appointment.schoolGate.index',$compact);
    }



    /**
     * 创建Banner
     *
     * @param Request $request
     * @return Application|Factory|View|RedirectResponse
     */
    public function add(Request $request)
    {
        if($request->isMethod('post')) {
            $admin_id = $request->get('admin_id');
            $table = 'school_gate';
            #表单验证
            $validator = AdminFormVerify($table, $request);
            if ($validator->fails()) {
                return $this->retError($validator,$request);
            }
            $name = $request->get('name');
            $post = $request->all();
//            dd($post);
            unset($post['_token']);
            $create = $post;
            $logs = [
                'admin_id' => $admin_id,
                'operate_menu' => OperationalLogs::$operateItem['appointment_manage'],
                'operate_item' => $name,
                'operate_content' => '添加Banner：' . $name,
            ];
            DB::beginTransaction();
            try {
                SchoolGate::query()->create($create);
                #操作日志
                OperationalLogs::AddOperationalLogs($logs);
                DB::commit();
                return $this->retSuccess();
            } catch (\Exception $exception) {
                DB::rollback();
                return $this->retError($exception->getMessage(),$request);
            }
        }
        $url = route('v1.admin.appointment.schoolGate.index');
        $params = [
            'url'=>$url,
            'success'=>'success'
        ];
        $subtitle = '新增校门';
        $view = 'v1.admin.appointment.schoolGate.add';
        $compact = compact('subtitle','params');
        return view($view,$compact);
    }
    /**
     * 编辑岗位
     *
     * @param Request $request
     * @param $id
     * @return Application|Factory|View|RedirectResponse
     */
    public function edit(Request $request,$id)
    {
        $model = SchoolGate::query();
        $where = [
            'id'=>$id
        ];
        $model_query = AdminModelQuery($id,$model,$where);
        if($model_query['code'] == 0){
            return  $this->retError($model_query['msg'],$request);
        }
        if($request->isMethod('post')) {
            $admin_id = $request->get('admin_id');
            $table = 'school_gate';
            $validator = AdminFormVerify($table, $request, 'update');
            if ($validator->fails()) {
                return $this->retError($validator,$request);
            }
            $name = $request->get('name');
            $where_unique = [
                'name'=>$name,
            ];
            $Model = SchoolGate::query();
            $data_unique = AdminUpdateDataUniqueVerify($Model,$id,$where_unique);
            if($data_unique['code'] == 201){
                return $this->retError($data_unique['msg'],$request);
            }
            $post = $request->all();
            unset($post['_token']);
            unset($post['id']);
            $update = $post;
            $logs = [
                'admin_id' => $admin_id,
                'operate_menu' => OperationalLogs::$operateItem['appointment_manage'],
                'operate_item' => $name,
                'operate_content' => '修改校门信息为' . $name,
            ];
            DB::beginTransaction();
            try {
                SchoolGate::query()->where('id', $id)->update($update);
                #操作日志
                OperationalLogs::AddOperationalLogs($logs);
                DB::commit();
                return $this->retSuccess();
            } catch (\Exception $exception) {
                DB::rollback();
                return $this->retError($exception->getMessage(), $request);
            }
        }
        $url = route('v1.admin.appointment.schoolGate.index');
        $params = [
            'url'=>$url,
            'success'=>'success'
        ];
        $subtitle = '编辑校门';
        $view = 'v1.admin.appointment.schoolGate.edit';
        $data = SchoolGate::getDataByID($id);
        $compact = compact('subtitle','params','data');
        return view($view,$compact);
    }


    /**
     * 修改校门状态
     *
     * @param Request $request
     * @param $id
     * @return array|RedirectResponse
     */
    public function changeStatus(Request $request,$id)
    {
        $model = SchoolGate::query();
        $where = [
            'id'=>$id
        ];
        $model_query = AdminModelQuery($id,$model,$where);
        if($model_query['code'] == 0){
            return  $this->retError($model_query['msg'],$request);
        }
        $status = SchoolGate::query()->where($where)->value('status');
        $status_update = 2;
        if($status == 2){
            $status_update = 1;
        }
        $logs = [
            'admin_id'=>$request->get('admin_id'),
            'operate_menu'=>OperationalLogs::$operateItem['appointment_manage'],
            'operate_item'=>SchoolGate::query()->where($where)->value('name').'('.$id.')',
            'operate_content'=>OperationalLogs::$operate['school_gate_status_change'].'为：'.getStatusArray()[$status_update],
        ];
        DB::beginTransaction();
        try {
            SchoolGate::query()->where($where)->update([
                'status'=>$status_update,
            ]);
            #添加操作日志
            OperationalLogs::AddOperationalLogs($logs);
            DB::commit();
            return AdminRet('操作成功!');
        } catch (\Exception $exception) {
            DB::rollBack();
            return AdminRetError($exception->getMessage());
        }
    }

    /**
     * 删除校门
     *
     * @param Request $request
     * @param $id
     * @return array
     * @throws \Throwable
     */
    public function delete(Request $request,$id): array
    {
        $where = [
            'id'=>$id,
        ];
        $data = SchoolGate::query()->where($where);
        $logs = [
            'admin_id'=>$request->get('admin_id'),
            'operate_menu'=>OperationalLogs::$operateItem['appointment_manage'],
            'operate_item'=>SchoolGate::query()->where($where)->value('name').'('.$id.')',
            'operate_content'=>OperationalLogs::$operate['school_gate_delete'],
        ];
        return AdminDeleteData($data,$logs);
    }

}
